/**
 * 
 */
package com.greatwideweb.runhelper;

import java.util.ArrayList;


import com.greatwideweb.runhelper.db.DatabaseAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * @author dave
 *
 */
public class DataScreen extends Activity implements OnItemClickListener 
{

  /**
   * 
   */
  
  private ArrayList<RunItem> items=null;
  private ListView listView=null;
  private DataArrayAdapter arrayAdapter=null;
  private Intent addScreenIntent;

  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_data);
    listView = (ListView) findViewById(R.id.listView1);
    addScreenIntent = new Intent(this,  AddScreen.class);
  }
  
  @Override
  protected void onResume()
  {
    super.onResume();
    DatabaseAdapter dbAdapter = new DatabaseAdapter(this).open();
    items = dbAdapter.fetchAll();
    dbAdapter.close();
    arrayAdapter = new DataArrayAdapter(this, R.layout.data_item, items);
    if(arrayAdapter != null) 
    {
      listView.setAdapter(arrayAdapter);
      listView.setOnItemClickListener(this);
    }
  }
  
  public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) 
  {
    RunItem item = items.get(index);
    Log.d("onItemClick", item.toString());
    addScreenIntent.putExtra(Application.SELECT_ITEM, item);
    startActivityForResult(addScreenIntent, 0);
    
    
  }
  

}
